package models

import (
	orm "go-admin/common/global"
	"gorm.io/gorm"
	"time"
)

type TOrder struct {
	ID             uint   `json:"id" gorm:"primarykey"`
	UserId         int64  `json:"userId" gorm:"type:bigint;comment:UserId"`    //
	OrderNo        string `json:"orderNo" gorm:"type:varchar(64);comment:订单号"` //
	PayType        int    `json:"payType" gorm:"type:int;comment:PayType"`     //
	Type           int    `json:"type"`
	Amount         int `json:"amount" gorm:"type:varchar(32);comment:Amount"` //
	Status         int    `json:"status" gorm:"type:varchar(32);comment:Status"` //
	Product        string `json:"product"`
	Url            string `json:"url" gorm:"-"`
	RightsId       int    `json:"rightsId" gorm:"-"`
	UserName       int    `json:"username" gorm:"-"`
	CreatedAt      time.Time `json:"createdAt"`
	UpdatedAt      time.Time `json:"updatedAt"`
	RechargeAmount int `json:"rechargeAmount" gorm:"-"`
	StatusResult string`json:"statusResult" gorm:"-"`
}

type TOrderPage struct {
	ID             uint   `gorm:"primarykey"`
	UserId         int64  `json:"userId" gorm:"type:bigint;comment:UserId"`    //
	OrderNo        string `json:"orderNo" gorm:"type:varchar(64);comment:订单号"` //
	PayType        int    `json:"payType" gorm:"type:int;comment:PayType"`     //
	Type           int    `json:"type"`
	Amount         int    `json:"amount" gorm:"type:varchar(32);comment:Amount"` //
	Status         int    `json:"status" gorm:"type:varchar(32);comment:Status"` //
	Product        string `json:"product"`
	MinusAmount    int    `json:"minusAmount" gorm:"type:varchar(32);comment:MinusAmount"` //
	Url            string `json:"url" gorm:"-"`
	RightsId       int    `json:"rightsId" gorm:"-"`
	Username       string `json:"username"`
	CreatedAt      time.Time `json:"createdAt"`
	UpdatedAt      time.Time `json:"updatedAt"`
	RechargeAmount int `json:"rechargeAmount" gorm:"-"`
	StatusResult string`json:"statusResult" gorm:"-"`
}

func (TOrder) TableName() string {
	return "t_order"
}

func (e *TOrder) GetId() interface{} {
	return e.ID
}

//添加
func (e *TOrder) Insert(tx *gorm.DB) (err error) {
	if err = tx.Table(e.TableName()).Create(&e).Error; err != nil {
		return
	}
	return
}

func (e *TOrder) InsertRechargeOrder(tx *gorm.DB) (err error) {
	if err = tx.Table(e.TableName()).Create(&e).Error; err != nil {
		return
	}
	return
}

func (e *TOrder) GetPage(pageSize int, pageIndex int) (orders []TOrderPage, count int64, err error) {
	table := orm.GetDb().Select("t_order.user_id as user_id, t_order.order_no, t_order.amount, " +
		"t_order.type, t_order.status, t_order.created_at, t_order.product, sys_user.username").Table(e.
		TableName())
	table = table.Joins("left join sys_user on t_order.user_id = sys_user.user_id")

	if e.UserId != 0 {
		table = table.Where("t_order.user_id = ?", e.UserId)
	}

	if e.Type != 0 {
		table = table.Where("t_order.type = ?", e.Type)
	}

	if err = table.Order("t_order.id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&orders).
		Offset(-1).Limit(-1).Count(&count).Error; err != nil {
		return nil, 0, err
	}
	return
}

func (e *TOrder) Get(orderNo string) (record TOrder, err error) {
	if err = orm.GetDb().Table(e.TableName()).First(&record, "order_no = ?", orderNo).Error; err != nil {
		return
	}
	return
}

func (e *TOrder) GetById() (record TOrder, err error) {
	if err = orm.GetDb().Table(e.TableName()).First(&record, "id = ?", e.ID).Error; err != nil {
		return
	}
	return
}

//修改
func (e *TOrder) Update(tx *gorm.DB, orderNo string) (err error) {
	var update TOrder
	if err = tx.Table(e.TableName()).First(&update, "order_no = ?", orderNo).Error; err != nil {
		return
	}

	if err = tx.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}
